package com.zking.ssm.controller;

import com.zking.ssm.model.HotelBrand;
import com.zking.ssm.service.IHotelBrandService;
import com.zking.ssm.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController  //返回json格式的数据
@RequestMapping("/Brand") //窄化路径
public class HotelBrandController {

    @Autowired
    private IHotelBrandService iHotelBrandService;

//    @RequestMapping("/list")
////    public Map<String,Object> list(HotelBrand hotelBrand){
////        System.out.println(hotelBrand);
////
////        PageBean pageBean = new PageBean();
////        pageBean.setRows(1);
////        pageBean.setPage(1);
////        List<HotelBrand> hotelBrands1 = iHotelBrandService.SelectAll(hotelBrand,pageBean);
////
////        Map<String,Object> map = new HashMap<>();
////
////        for(HotelBrand s : hotelBrands1){
////            System.out.println(s);
////        }
////
////        map.put("Result",hotelBrands1);
////
////        return map;
////    }

    @RequestMapping("/list")
    public Map<String,Object> list(HotelBrand hotelBrand, HttpServletRequest request,PageBean pageBean){

            Map<String, Object> map = new HashMap<String, Object>();
            pageBean.setRequest(request);
            pageBean.setPage(pageBean.getPage());
            pageBean.setRows(pageBean.getRows());
            pageBean.setTotal(pageBean.getTotal());
            List<HotelBrand> userList = iHotelBrandService.SelectAll(hotelBrand, pageBean);
            map.put("code", 0);
            map.put("result", userList);
            map.put("page", pageBean.getPage());
            map.put("rows", pageBean.getRows());
            map.put("total", pageBean.getTotal());
            return map;
    }

    @RequestMapping("/add")
    public Map<String,Object> add(HotelBrand hotelBrand){
        System.out.println(hotelBrand);
        int n = iHotelBrandService.insert(hotelBrand);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","增加品牌成功！");
        }else{
            map.put("code",-1);
            map.put("message","增加品牌失败！");
        }
        return map;
    }

    @RequestMapping("/del")
    public Map<String,Object> del(HotelBrand hotelBrand){
        System.out.println(hotelBrand);
        int n = iHotelBrandService.deleteByPrimaryKey(hotelBrand.getId());
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","删除品牌成功！");
        }else{
            map.put("code",-1);
            map.put("message","删除品牌失败！");
        }
        return map;
    }

    @RequestMapping("/update")
    public Map<String,Object> update(HotelBrand hotelBrand){
        int n = iHotelBrandService.updateByPrimaryKeySelective(hotelBrand);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","修改品牌成功！");
        }else{
            map.put("code",-1);
            map.put("message","修改品牌失败！");
        }
        return map;
    }

    @RequestMapping("/get")
    public Map<String,Object> get(HotelBrand hotelBrand){
        HotelBrand user = iHotelBrandService.selectByPrimaryKey(hotelBrand.getId());
        Map<String,Object> map = new HashMap<>();
        if(user != null){
            map.put("code",0);
            map.put("message","查询品牌成功！");
            map.put("request",user);
        }else{
            map.put("code",-1);
            map.put("message","查询品牌失败！");
        }
        return map;
    }

}
